package com.lottery.game.core.utils;

import com.lottery.game.core.modules.entity.user.BetLimit;
import com.lottery.game.core.modules.entity.user.SpecialOdds;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TimeZone;

public class BetUtils {

    //基础项目
    public static List<String> baseBetItemList = Arrays.asList("da","xiao","dan","shuang");
    //组合项目
    public static List<String> combinationBetItemList = Arrays.asList("dadan","xiaodan","dashuang","xiaoshuang");

    public static List<String> temaBetItemList = Arrays.asList("tema0","tema1","tema2","tema3","tema4","tema5","tema6","tema7","tema8","tema9","tema10","tema11","tema12","tema13","tema14","tema15","tema16","tema17","tema18","tema19","tema20","tema21","tema22","tema23","tema24","tema25","tema26","tema27");

    public static List<String> jizhiBetItemList = Arrays.asList("jida","jixiao");

    public static List<String> boseBetItemList = Arrays.asList("lanbo","lvbo","hongbo");

    public static List<String> baoziduiziBetItemList = Arrays.asList("baozi","duizi","shunzi");

    //映射map
//    public static Map<String, String> betStrMap = new HashMap<String, String>();
//
//    static {
//        betStrMap.put("da", "大");
//    }
	/**
	 * 根据竞猜结果和用户下注获取金额
	 * 未猜中为0
	 * @param resultBet
	 * @param userBet
	 * @return
	 */
	public static BigDecimal getResultMoney(Map<String, Object> oddRecord,Map<String, String> resultBet,Map<String, Object> userBet){
		BigDecimal bonus = BigDecimal.ZERO;
		if(oddRecord==null||resultBet==null||userBet==null) {
			return bonus;
		}
		Iterator<Entry<String, String>> it = resultBet.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, String> resultEntry = it.next();
			String key = resultEntry.getKey();
			//String resultValue = resultEntry.getValue();
			Object oddValue = oddRecord.get(key);
			Object userValue = userBet.get(key);
			Object pointObj = userBet.get("point");
			if(oddValue!=null&&userValue!=null){
				BigDecimal tmp = (new BigDecimal(oddValue.toString()).multiply(new BigDecimal(userValue.toString()).multiply(new BigDecimal(pointObj.toString()))));
				bonus = bonus.add(tmp);
			}
		}
		bonus.setScale(2);
		return bonus;
	}

	/**
	 * 期次加1
	 * @param expect
	 * @return
	 */
	public static String getNextExpect(String expect) {
		Integer expectInt = Integer.parseInt(expect);
		return String.valueOf((expectInt + 1));
	}

	/**
	 * 获取前一期
	 * @param expect
	 * @return
	 */
	public static String getEarlyExpect(String expect){
        Integer expectInt = Integer.parseInt(expect);
        return String.valueOf((expectInt-1));
    }

	public static int getExpectNum(String expect) {
	    Integer expectInt = Integer.parseInt(expect);
        return (expectInt+1);
	}

//	/**
//	 * 改为使用DataUtils里获取下期开奖时间
//	 * @param date
//	 * @return
//	 */
//	public static Date getNextOpenTime(int gameTime,Date date){
//	    Calendar calendar = Calendar.getInstance();
//	    calendar.setTime(date);
//	    calendar.add(Calendar.SECOND, gameTime);
//	    return calendar.getTime();
//	}

	/**
	 * 加秒钟
	 * @return
	 */
	public static Date addSeconds(Date date,int seconds) {
	    Calendar calendar = Calendar.getInstance();
      calendar.setTime(date);
      calendar.add(Calendar.SECOND, seconds);
      return calendar.getTime();
	}


	/**
	 *
	 * @param val
	 * @return
	 */
	public static boolean isBlank(BigDecimal val) {
	    boolean flag = val == null? true: BigDecimal.ZERO.equals(val);
	    return flag;
	}

	/**
	 * 当开出13-14时根据用户下注金额调整赔率
	 * @date 2018-07-30
	 * @zhuiyv 2018-07-30
	 */
    public static Map<String, Object> fixOddMapByUserBet(Map<String, Object> oddMap, SpecialOdds specialOdds,
            Map<String, Object> userBet) {
        BigDecimal point = new BigDecimal(userBet.get("sameTotle").toString());
        point = point==null?BigDecimal.ZERO:point;
        String betStr = userBet.get("betStr").toString();
        String[] splitBetArr = betStr.split(",");

        for(String tmpBetStr :splitBetArr) {
            //用户下注的基础项目
            if(baseBetItemList.contains(tmpBetStr)) {
                if(point.compareTo(specialOdds.getDxdsSum1())<=0) {
                    oddMap.put(tmpBetStr, specialOdds.getDxdsPrice1());
                }else if(point.compareTo(specialOdds.getDxdsSum1())>0&&point.compareTo(specialOdds.getDxdsSum2())<=0) {
                    oddMap.put(tmpBetStr, specialOdds.getDxdsPrice2());
                }else if(point.compareTo(specialOdds.getDxdsSum2())>0) {
                    oddMap.put(tmpBetStr, specialOdds.getDxdsPrice3());
                }
            }

            //用户下注的组合项目
            else if(combinationBetItemList.contains(tmpBetStr)) {
                if(point.compareTo(specialOdds.getCombinationSum1())<=0) {
                    oddMap.put(tmpBetStr, specialOdds.getCombinationPrice1());
                }else if(point.compareTo(specialOdds.getCombinationSum1())>0&&point.compareTo(specialOdds.getCombinationSum2())<=0) {
                    oddMap.put(tmpBetStr, specialOdds.getCombinationPrice2());
                }else if(point.compareTo(specialOdds.getCombinationSum2())>0) {
                    oddMap.put(tmpBetStr, specialOdds.getCombinationPrice3());
                }
            }
        }
        oddMap.put("duizi", specialOdds.getDuiziPrice());
        return oddMap;
    }

    public static Integer getMaxAmtByBetStr(BetLimit limitInfo,String betStr){
        Integer resultInt = null;
        if(baseBetItemList.contains(betStr)) {
            resultInt =  limitInfo.getDaxiaodanshuangMax();
        }
        if(combinationBetItemList.contains(betStr)) {
            resultInt = limitInfo.getZuhedanshuangMax();
        }
        if(temaBetItemList.contains(betStr)) {
            resultInt = limitInfo.getTemaMax();
        }
        if(jizhiBetItemList.contains(betStr)) {
            resultInt = limitInfo.getJizhiMax();
        }
        if(boseBetItemList.contains(betStr)) {
            resultInt = limitInfo.getBosheMax();
        }
        if(baoziduiziBetItemList.contains(betStr)) {
            resultInt = limitInfo.getBaoziduiziMax();
        }
        return resultInt;
    }

    public static Integer getMinAmtByBetStr(BetLimit limitInfo,String betStr){
        Integer resultInt = null;
        if(baseBetItemList.contains(betStr)) {
            resultInt =  limitInfo.getDaxiaodanshuangMin();
        }
        if(combinationBetItemList.contains(betStr)) {
            resultInt = limitInfo.getZuhedanshuangMin();
        }
        if(temaBetItemList.contains(betStr)) {
            resultInt = limitInfo.getTemaMin();
        }
        if(jizhiBetItemList.contains(betStr)) {
            resultInt = limitInfo.getJizhiMin();
        }
        if(boseBetItemList.contains(betStr)) {
            resultInt = limitInfo.getBosheMin();
        }
        if(baoziduiziBetItemList.contains(betStr)) {
            resultInt = limitInfo.getBaoziduiziMin();
        }
        return resultInt;
    }
    /**
     * 判断开奖结果是否是豹子顺子对子
     * @param map
     * @return
     */
    public static boolean isBaoziShunziDuizi(Map<String, String> map) {
        boolean flag  = false;
        if(map!=null) {
            String trueStr = "true";
            if(trueStr.equals(map.get("baozi"))||trueStr.equals(map.get("shunzi"))||trueStr.equals(map.get("duizi"))) {
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 判断下注字符串是否是豹子顺子对子
     * @param Str
     * @return
     */
    public static boolean isBaoziShunziDuizi(String tmpStr) {
        boolean flag  = false;
        if(tmpStr!=null) {
            if(tmpStr.equals("baozi")||tmpStr.equals("shunzi")||tmpStr.equals("duizi")) {
                flag = true;
            }
        }
        return flag;
    }

    public static boolean isBaseAndCom(String tmpStr) {
        boolean flag  = false;
        if(tmpStr!=null) {
            if(combinationBetItemList.contains(tmpStr)||baseBetItemList.contains(tmpStr)) {
                flag=true;
            }
        }
        return flag;
    }



    public static Map<String, Object> fixBaseOddMapByBaoziShunziDuizi(Map<String, Object> oddMap,SpecialOdds specialOdds) {
        for(String tmpStr :baseBetItemList) {
            oddMap.put(tmpStr, specialOdds.getBaoziduizishunziPrice());
        }
        for(String tmpStr :combinationBetItemList) {
            oddMap.put(tmpStr, specialOdds.getBaoziduizishunziPrice());
        }
        return oddMap;
    }

    /**
     * 判断是否在维护
     * 支持23:00 - 09:00 表示23点维护到第二天9点
     *   或  19:00-20:00  表示19维护到20点
     * @throws ParseException
     * @date 2018-08-07
     */
    public static int isShutDown(String nowStr,String beginTime,String endTime) throws ParseException {
        if(!StringUtils.isEmpty(nowStr)&&!StringUtils.isEmpty(beginTime)&&!StringUtils.isEmpty(endTime)) {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            if(beginTime.compareTo(endTime)<=0) {
                if(nowStr.compareTo(beginTime)>=0&&nowStr.compareTo(endTime)<=0) {
                    int downTime = (int) (sdf.parse(endTime).getTime() - sdf.parse(nowStr).getTime())/1000;
                    return  downTime;
                }
            }else {
                if(nowStr.compareTo(beginTime)>=0||nowStr.compareTo(endTime)<=0) {
                    int downTime = (int) (sdf.parse(endTime).getTime() - sdf.parse(nowStr).getTime())/1000;
                    //如果downTime小于0，则加上一天
                    if (downTime < 0) {
                        downTime = downTime + (24 * 3600);
                    }
                    return downTime;
                }
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        try {
           int i= isShutDown("23:57", "23:55", "09:06");
           System.out.println(i/60);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
